﻿//using System;
//using Brouteforce.gameobjs;
//using Brouteforce.managers;
//using Brouteforce.util;
//using Microsoft.Xna.Framework;
//using Brouteforce.utilities;
//using System.Collections.Generic;

//namespace Brouteforce.gameObjs.gamePiece {
//    public enum Direction {
//        Right,
//        Left,
//        Up,
//        Down,
//        None
//    }

//    public abstract class GamePiece {

//        public String DashbordMsg;
//        public int Speed;
//        public int Light;
//        public int Life;
//        public int Power;

//        public int NumberOfMovesLeft;
//        public int Velocity = 50;
//        public List<Vector2> MoveList = new List<Vector2>();
//        private Direction _lastDirection = Direction.None;
//        private int _belongsToPlayer;
//        public State State;
//        public const int ShotsPerTurn = 1;
//        public int ShotsLeft;

//        public Sprite Sprite;

//        public GamePiece(int speed, int light, int life, int power, Sprite sprite, int belongsToPlayer) {
//            Speed = speed;
//            Light = light;
//            Life = life;
//            Power = power;
//            Sprite = sprite;
//            NumberOfMovesLeft = speed;
//            BelongsToPlayer = belongsToPlayer;
//        }

//        public int BelongsToPlayer {
//            get { return _belongsToPlayer; }
//            set {
//                _belongsToPlayer = value;
//                if (_belongsToPlayer == 0)
//                    Sprite.BorderColor = Color.Red;
//                else if (_belongsToPlayer == 1)
//                    Sprite.BorderColor = Color.Blue;
//                else
//                    Sprite.BorderColor = null;
//            }
//        }

//        public void Update() {
//            UpdateMovement();
//            InnerUpdate();
//            MousePassedOnTile(WorldManager.TileUnderMouse);
//        }

//        protected abstract void InnerUpdate();

//        private void UpdateMovement() {

//            if (MoveList.Count == 0) {
//                GuardCastle();
//                return;
//            }

//            Vector2 tile = MoveList[0];
//            while (true) {
//                if (IsInside(tile)) {
//                    WorldManager.MoveGamePieceTo(this, WorldManager.GetPosInWorld(WorldManager.GetGamePieceTileInWorld(this)), tile);
//                    MoveList.Remove(tile);
//                    if (MoveList.Count == 0) {
//                        GuardCastle();
//                        return;
//                    }
//                    tile = MoveList[0];
//                    Sprite.Location.Velocity = Velocity * Tiles.GetVelocity(WorldManager.GetPosInWorld(WorldManager.GetGamePieceTileInWorld(this)), tile);
//                    _lastDirection = MovingDirection(tile);
//                } else {
//                    Sprite.Location.LinearMove();
//                    return;
//                }
//            }
//          }
//        //private void GuardCastle() {
//        //    if ((this is Defender) && (WorldManager.GetGamePieceTileInWorld(this)).Contain(typeof(Tower))) {
//        //        GamePiece tower = (WorldManager.GetGamePieceTileInWorld(this).ContainedGamePieces).Find(a => a is Tower);
//        //        if (tower.BelongsToPlayer == -1) {
//        //            tower.BelongsToPlayer = _belongsToPlayer;
//        //            PlayersManager.Players[_belongsToPlayer].AddTower(towers.Add((Tower)tower));
//        //            PlayersManager.Players[_belongsToPlayer].AddTower((Tower)tower);
//        //        }
//        //    }
//        //}


//        private void UnGuardCastle() {
//            if ((this is Defender) && (WorldManager.GetGamePieceTileInWorld(this)).Contain(typeof(Tower))) {
//                GamePiece tower = (WorldManager.GetGamePieceTileInWorld(this).ContainedGamePieces).Find(a => a is Tower);
//                if (tower.BelongsToPlayer == _belongsToPlayer) {
//                    tower.BelongsToPlayer = -1;
//                    PlayersManager.Players[_belongsToPlayer].towers.Remove((Tower)tower);
//                    PlayersManager.Players[_belongsToPlayer].RemoveTower((Tower)tower);
//                }
//            }
//        }

//        private Direction MovingDirection(Vector2 tile) {
//            var myTile = WorldManager.GetPosInWorld(WorldManager.GetGamePieceTileInWorld(this));
//            if (Tiles.GetBottomSibling(myTile) == tile)
//                return Direction.Down;
//            if (Tiles.GetTopSibling(myTile) == tile)
//                return Direction.Up;
//            if (Tiles.GetRightSibling(myTile) == tile)
//                return Direction.Right;
//            return Tiles.GetLeftSibling(myTile) == tile ? Direction.Left : _lastDirection;
//        }

//        private bool IsInside(Vector2 tile) {
//            var myTile = WorldManager.GetTileAt(tile).Sprite.Location.Position;
//            var tileCenter = myTile.Center;
//            var myCenter = Sprite.Location.Position.Center;
//            switch (_lastDirection) {
//                case Direction.Right:
//                    if (myCenter.X > tileCenter.X)
//                        return true;
//                    break;

//                case Direction.Left:
//                    if (myCenter.X < tileCenter.X)
//                        return true;
//                    break;

//                case Direction.Up:
//                    if (myCenter.Y < tileCenter.Y)
//                        return true;
//                    break;

//                case Direction.Down:
//                    if (myCenter.Y > tileCenter.Y)
//                        return true;
//                    break;

//                case Direction.None:
//                    return true;
//            }
//            return false;
//        }



//        public Location Location {

//            get { return Sprite.Location; }
//        }

//        public virtual void Draw(int cameraX, int cameraY) {
//            Sprite.Draw(cameraX, cameraY);
//        }

//        public abstract void Fire(Tile fromTile, Tile toTile);


//        public virtual int MoveTo(Tile fromTile, Tile toTile) {
//            if ((fromTile.XIndex < 0) || (fromTile.YIndex < 0) ||
//                (fromTile.YIndex > WorldManager.World.GetLength(0)) ||
//                (fromTile.YIndex > WorldManager.World.GetLength(1)) ||
//                (toTile.YIndex < 0) || (toTile.YIndex < 0) ||
//                (toTile.YIndex > WorldManager.World.GetLength(0)) ||
//                (toTile.YIndex > WorldManager.World.GetLength(1)))
//                return 0;
//            if (MoveList.Count > 0)
//                return 0;
//            MoveList = Tiles.FindPath(new Vector2(fromTile.XIndex, fromTile.YIndex), new Vector2(toTile.XIndex, toTile.YIndex), NumberOfMovesLeft);
//            if (MoveList.Count > NumberOfMovesLeft + 1) {
//                MoveList.Clear();
//                return 0;
//            }
//            int ans =  MoveList.Count - 1;
//            if (ans >= 1) UnGuardCastle();
//            return ans;

//        }

//        public void DecreaseLife(int amount) {
//            Life = Life - amount;
//        }

//        public abstract void MousePassedOnTile(Tile tile);

//        public double ComputeChances(Tile opponentGamePieceTile, Tile fromTile) {
//            int distance = Math.Abs(fromTile.XIndex - (int)opponentGamePieceTile.XIndex) +
//                           Math.Abs(fromTile.YIndex - (int)opponentGamePieceTile.YIndex);

//            //Computing chances
//            double lessChancePerUnit = (Shooter.MaxShootChances - Shooter.MinShootChances) / (Light - 1);
//            //Tile  = WorldManager.Instance.GetGamePieceTileInWorld(opponentGamePiece);

//            double computetChances = (Shooter.MaxShootChances - lessChancePerUnit * (distance - 1));
//            return computetChances;
//        }

//        public string HitChances(Tile opponentGamePieceTile, Tile fromTile) {
//            string hitChances;
//            bool isHighlighted = opponentGamePieceTile.IsHighlighted();
//            //ToDo check if there is an opponent piece
            
//            if (isHighlighted)
//                hitChances = "Chances of hitting are : " + ComputeChances(opponentGamePieceTile, fromTile);
//            else hitChances = "Out of Sight - Chances are : " + Shooter.OutOfSightChances;

//            return hitChances;
//        }
//    }
//}